package org.openmore.consumer.controller.api;

import io.swagger.annotations.*;
import org.openmore.common.exception.OpenmoreException;
import org.openmore.consumer.annotation.TokenAuthCheck;
import org.openmore.consumer.controller.common.BaseController;
import org.openmore.consumer.dto.api.PlatformConfigDto;
import org.openmore.consumer.dto.common.BaseResponse;
import org.openmore.consumer.entity.User;
import org.openmore.consumer.entity.enums.PlatformType;
import org.openmore.consumer.entity.enums.UserType;
import org.openmore.consumer.framework.MyThreadLocalConfig;
import org.openmore.consumer.service.feign.PlatformConfigService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import static org.springframework.http.MediaType.APPLICATION_JSON_UTF8_VALUE;

/**
 * 三方平台信息配置接口
 * 参考：
 * @author <a href="https://github.com/binarywang">Binary Wang</a>
 */
@Api(value = "/PlatformConfig", tags = "PlatformConfig", description = "三方平台信息配置接口")
@RequestMapping(value = "/api/platformconfig", produces = {APPLICATION_JSON_UTF8_VALUE})
@RestController
public class PlatformConfigController extends BaseController {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private PlatformConfigService platformConfigService;

    /**
     * 查询配置的平台信息
     */
    @TokenAuthCheck
    @ApiOperation(value = "查询配置的平台信息", response = String.class)
    @ApiResponses(value = {@ApiResponse(code = 4000, message = "请求失败：原因", response = String.class)})
    @RequestMapping(method = RequestMethod.GET, value = "/select")
    public BaseResponse selectAll(@RequestParam(required = false) @ApiParam(name = "应用ID") String tenantId,
                          @RequestParam(required = false) @ApiParam(name = "平台类型")PlatformType platformType,
                          @RequestParam(required = false) @ApiParam(name = "三方平台账户appid")String appid,
                          @RequestParam(required = false) @ApiParam(name = "微信商户平台mchKey")String mchKey,
                          @RequestParam(required = false, defaultValue = "1") @ApiParam(name = "页码")Integer pageNum,
                          @RequestParam(required = false,  defaultValue = "10") @ApiParam(name = "每页条数") Integer pageSize) {

        return platformConfigService.selectAll(tenantId, platformType, appid, mchKey, pageNum, pageSize);
    }

    /**
     * 查询配置的平台信息数量
     */
    @TokenAuthCheck
    @ApiOperation(value = "查询配置的平台信息数量", response = String.class)
    @ApiResponses(value = {@ApiResponse(code = 4000, message = "请求失败：原因", response = String.class)})
    @RequestMapping(method = RequestMethod.GET, value = "/count")
    public BaseResponse selectCount(@RequestParam(required = false) @ApiParam(name = "应用ID") String tenantId,
                          @RequestParam(required = false) @ApiParam(name = "平台类型")PlatformType platformType,
                          @RequestParam(required = false) @ApiParam(name = "三方平台账户appid")String appid,
                          @RequestParam(required = false) @ApiParam(name = "微信商户平台mchKey")String mchKey) {

        return platformConfigService.selectCount(tenantId, platformType, appid, mchKey);
    }
    /**
     * 保存平台配置信息
     */
    @TokenAuthCheck
    @ApiOperation(value = "保存平台配置信息", response = String.class)
    @ApiResponses(value = {@ApiResponse(code = 4000, message = "请求失败：原因", response = String.class)})
    @RequestMapping(method = RequestMethod.POST, consumes = {APPLICATION_JSON_UTF8_VALUE})
    public BaseResponse insert(@RequestBody @ApiParam("平台配置信息") PlatformConfigDto platformConfig) {
        User user = MyThreadLocalConfig.getUser();
        if(null == user || user.getUserType() == UserType.USER){
            throw new OpenmoreException(400, "权限非法");
        }
        return platformConfigService.insert(platformConfig);
    }
    /**
     * 更新平台配置信息
     */
    @TokenAuthCheck
    @ApiOperation(value = "更新平台配置信息", response = String.class)
    @ApiResponses(value = {@ApiResponse(code = 4000, message = "请求失败：原因", response = String.class)})
    @RequestMapping(method = RequestMethod.PUT, consumes = {APPLICATION_JSON_UTF8_VALUE})
    public BaseResponse update(@RequestBody @ApiParam("平台配置信息")PlatformConfigDto platformConfig) {
        User user = MyThreadLocalConfig.getUser();
        if(null == user || user.getUserType() == UserType.USER){
            throw new OpenmoreException(400, "权限非法");
        }
        platformConfigService.update(platformConfig);
        return BaseResponse.buildSuccessResponse("更新成功");
    }
    /**
     * 删除平台配置信息
     */
    @TokenAuthCheck
    @ApiOperation(value = "删除平台配置信息", response = String.class)
    @ApiResponses(value = {@ApiResponse(code = 4000, message = "请求失败：原因", response = String.class)})
    @RequestMapping(method = RequestMethod.DELETE, value = "/{id}")
    public BaseResponse update(@PathVariable @ApiParam("配置项ID")String id) {
        User user = MyThreadLocalConfig.getUser();
        if(null == user || user.getUserType() == UserType.USER){
            throw new OpenmoreException(400, "权限非法");
        }
        platformConfigService.deleteById(id);
        return BaseResponse.buildSuccessResponse("更新成功");
    }

    /**
     * 查询单条平台配置信息
     */
    @TokenAuthCheck
    @ApiOperation(value = "查询单条平台配置信息", response = String.class)
    @ApiResponses(value = {@ApiResponse(code = 4000, message = "请求失败：原因", response = String.class)})
    @RequestMapping(method = RequestMethod.GET, value = "/{id}")
    public BaseResponse queryById(@PathVariable @ApiParam("配置项ID")String id) {
        return BaseResponse.buildSuccessResponse(platformConfigService.queryById(id));
    }
}